package cn.doctor.assistant.pad.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class ConversationDBManager extends DBBase{
	
	public static final String table = "conversation_list";
	
	private static ConversationDBManager instance;
	public synchronized static ConversationDBManager getInstance(){
		if(instance == null){
			instance = new ConversationDBManager();
		}
		return instance;
	}
	
	public synchronized ArrayList<String> getConversationList(){
		ArrayList<String> res = new ArrayList<String>();
		SQLiteDatabase sqlDB = DBHelper.getSQLiteDatabase();
		if(!tableIsExist(sqlDB, table)) return res;
		
		Cursor cursor = sqlDB.rawQuery("select * from conversation_list", null);
		if(cursor.moveToFirst()){
			do{
				String name = null;
				// username
				name = cursor.getString(0);
				
				if(name == null){
					// groupname
					name = cursor.getString(1);
				}
				res.add(name);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return res;
	}
	
	/*public Hashtable<String, EMConversation> c() {
		Hashtable localHashtable = new Hashtable();
		try {
			SQLiteDatabase localSQLiteDatabase = DBHelper.getSQLiteDatabase();
			String str1 = "SELECT *, MAX(msgtime), COUNT(*) AS msgCount FROM chat AS A  where A.groupname is null and A.participant in (select username from conversation_list) GROUP BY participant ";
			String str2 = "SELECT *, MAX(msgtime), COUNT(*) AS msgCount FROM chat AS A  where A.groupname is not null and A.groupname in (select groupname from conversation_list) GROUP BY groupname ";
			String[] arrayOfString1 = { str1, str2 };
			for (String str3 : arrayOfString1) {
				Cursor localCursor = localSQLiteDatabase.rawQuery(str3, null);
				if (!localCursor.moveToFirst()) {
					localCursor.close();
				} else {
					do {
						EMMessage localEMMessage = b(localCursor);
						long l1 = localCursor.getLong(localCursor.getColumnIndex("msgCount"));
						LinkedList localLinkedList = new LinkedList();
						localLinkedList.add(localEMMessage);
						boolean bool = !localCursor.isNull(localCursor.getColumnIndex("groupname"));
						String str4 = a(localCursor);
						EMConversation.EMConversationType localEMConversationType = a(bool, str4, localSQLiteDatabase);
						EMConversation localEMConversation = new EMConversation(str4, localLinkedList,
								localEMConversationType, Long.valueOf(l1));
						localHashtable.put(str4, localEMConversation);
					} while (localCursor.moveToNext());
					localCursor.close();
				}
			}
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return localHashtable;
	}

	private EMConversation.EMConversationType b(int paramInt) {
		if (paramInt == EMConversation.EMConversationType.Chat.ordinal()) {
			return EMConversation.EMConversationType.Chat;
		}
		if (paramInt == EMConversation.EMConversationType.ChatRoom.ordinal()) {
			return EMConversation.EMConversationType.ChatRoom;
		}
		if (paramInt == EMConversation.EMConversationType.GroupChat.ordinal()) {
			return EMConversation.EMConversationType.GroupChat;
		}
		if (paramInt == EMConversation.EMConversationType.HelpDesk.ordinal()) {
			return EMConversation.EMConversationType.HelpDesk;
		}
		return EMConversation.EMConversationType.Chat;
	}

	public Hashtable<String, EMConversation> a(int paramInt) {
		Hashtable<String, EMConversation> localHashtable = new Hashtable<>();
		EMMessage localEMMessage = null;
		try {
			SQLiteDatabase localSQLiteDatabase = DBHelper.getSQLiteDatabase();
			String str1 = "select * from chat where groupname is null and participant in (select username from conversation_list) order by participant, msgtime desc";
			String str2 = "select * from chat where groupname is not null and groupname in (select groupname from conversation_list) order by groupname, msgtime desc";
			String[] arrayOfString1 = { str1, str2 };
			for (String str3 : arrayOfString1) {
				Cursor localCursor = localSQLiteDatabase.rawQuery(str3, null);
				if (!localCursor.moveToFirst()) {
					localCursor.close();
				} else {
					Object localObject1 = null;
					Object localObject2 = null;
					long l1 = 0L;
					boolean bool = false;
					EMConversation.EMConversationType localEMConversationType = EMConversation.EMConversationType.Chat;
					Object localObject3;
					do {
						localObject3 = a(localCursor);
						if ((localObject2 != null) && (((String) localObject2).equals(localObject3))) {
							if (((List) localObject1).size() < paramInt) {
								localEMMessage = b(localCursor);
								((List) localObject1).add(localEMMessage);
							}
							l1 += 1L;
						} else if ((localObject2 == null) || (!((String) localObject2).equals(localObject3))) {
							if (localObject2 != null) {
								Collections.reverse((List) localObject1);
								EMConversation localEMConversation = new EMConversation((String) localObject2,
										(List) localObject1, localEMConversationType, Long.valueOf(l1));
								localHashtable.put(localObject2, localEMConversation);
							}
							localObject1 = new LinkedList();
							((List) localObject1).add(b(localCursor));
							bool = !localCursor.isNull(localCursor.getColumnIndex("groupname"));
							localObject2 = localObject3;
							l1 = 1L;
							localEMConversationType = a(bool, (String) localObject3, localSQLiteDatabase);
						}
					} while (localCursor.moveToNext());
					if (localObject2 != null) {
						Collections.reverse((List) localObject1);
						localObject3 = new EMConversation((String) localObject2, (List) localObject1,
								localEMConversationType, Long.valueOf(l1));
						localHashtable.put(localObject2, localObject3);
					}
					localCursor.close();
				}
			}
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return localHashtable;
	}*/
}
